package com.google 
{
	import com.google.zxing.BarcodeFormat;
	import com.google.zxing.BinaryBitmap;
	import com.google.zxing.BufferedImageLuminanceSource;
	import com.google.zxing.client.result.ResultParser;
	import com.google.zxing.common.flexdatatypes.HashTable;
	import com.google.zxing.common.GlobalHistogramBinarizer;
	import com.google.zxing.DecodeHintType;
	import com.google.zxing.qrcode.QRCodeReader;
	import com.google.zxing.Result;
	import com.logs;
	import flash.display.BitmapData;
	import flash.utils.ByteArray;
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class ReadQr 
	{
		
		public function ReadQr() 
		{
			
		}
		
		private static  function getAllHints():HashTable
		{
			var ht:HashTable=new HashTable;
			ht.Add(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
			return ht;
		}
		public static  function decodeBitmapData(bmpd:BitmapData):String
		{
			var lsource:BufferedImageLuminanceSource=new BufferedImageLuminanceSource(bmpd);
			var bitmap:BinaryBitmap=new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(bmpd)));
			var res:Result=null;
			try {
				var qrReader:QRCodeReader = new QRCodeReader();
				res=qrReader.decode(bitmap, getAllHints());
			}catch (e:Error) {
				return null;
			}
			if (res == null) {
				return null;
			}else {
				var str:String = (ResultParser.parseResult(res).getDisplayResult());
			}
			var bytes:ByteArray = new ByteArray();
			bytes.writeMultiByte(str, "ISO-8859-1");
			trace(bytes, bytes.length);
			bytes.position = 0;
			trace( bytes.bytesAvailable);
			str = bytes.readMultiByte(bytes.bytesAvailable, "utf-8");
			logs.adds(str);
			return str;
		}
	}

}
